<?php
	class Agent extends MY_controller{

		function __construct(){
			parent::__construct();
		}

		function index($school_id = 0, $limit = 0){
			$this->load->model("Schoolo2o_model");
			$post_data = $this->input->post();
			if(!empty($post_data['school_id'])){
				$school_id = $post_data['school_id'];
			}
			$conditions['school_id'] = $school_id;
			$school_list = $this->Schoolo2o_model->get_school_agent($limit, $this->offset, $conditions);
			$this->load->library('pagination');
			$page_config['base_url'] = '/agent/index/'.$school_id;
			$page_config['total_rows'] = $school_list['count'];
			$page_config['per_page'] = $this->offset;
			$page_config['uri_segment'] = 4;
			$this->pagination->initialize($page_config);
			$pagination = $this->pagination->create_links();
			if(!empty($school_list['count'])){
				$this->load->model("Agents");
				foreach ($school_list['result'] as $key => $value) {
					$school_list['result'][$key]['video_price'] = $this->Agents->get_school_recode(1, $value['id']);
					$video_commission_used = $this->Agents->get_school_recode(1, $value['id'], 1);
					$school_list['result'][$key]['video_price']['commission_used'] = $video_commission_used['commission'];

					$school_list['result'][$key]['topic_price'] = $this->Agents->get_school_recode(4, $value['id']);
					$topic_commission_used = $this->Agents->get_school_recode(4, $value['id'], 1);
					$school_list['result'][$key]['topic_price']['commission_used'] = $topic_commission_used['commission'];

					$school_list['result'][$key]['shixun_price'] = $this->Agents->get_school_recode(5, $value['id']);
					$shixun_commission_used = $this->Agents->get_school_recode(5, $value['id'], 1);
					$school_list['result'][$key]['shixun_price']['commission_used'] = $shixun_commission_used['commission'];
				}
			}
			// print_r($school_list);exit;
			$school = $this->Schoolo2o_model->get_school_angent_list();

			$this->smarty->assign("menu", 'agent');
			$this->smarty->assign("conditions", $conditions);
			$this->smarty->assign("schools", $school);
			$this->smarty->assign("action", '下级机构列表');
			$this->smarty->assign("item_list", $school_list['result']);
			$this->smarty->assign("pagination", $pagination);
			$this->smarty->display("agent/list.html");
		}

		function detail($school_id = 0){
			$this->load->model("Schoolo2o_model");
			$school_info = get_field_by_where("o2o_school", "*", "id='{$school_id}'");
			$school_info['no_use_course'] = get_field_by_where("o2o_school_system_video_course_codes", "count(id) AS count", "school_id='{$school_id}' AND status = 1");
			$school_info['y_use_course'] = get_field_by_where("o2o_school_system_video_course_codes", "count(id) AS count", "school_id='{$school_id}' AND status = 2");
			$school_info['no_use_topic'] = get_field_by_where("o2o_school_system_topic_codes", "count(id) AS count", "school_id='{$school_id}' AND status = 1");
			$school_info['y_use_topic'] = get_field_by_where("o2o_school_system_topic_codes", "count(id) AS count", "school_id='{$school_id}' AND status = 2");
			$school_info['no_use_shixun'] = get_field_by_where("o2o_school_system_shixun_codes", "count(id) AS count", "school_id='{$school_id}' AND status = 1");
			$school_info['y_use_shixun'] = get_field_by_where("o2o_school_system_shixun_codes", "count(id) AS count", "school_id='{$school_id}' AND status = 2");
			$this->smarty->assign("school_info", $school_info);
			// print_r($school_info);
			$this->smarty->display("agent/detail.html");

		}
	}